package com.lagou.dao.impl;

import com.lagou.dao.AccountDao;
import com.lagou.domain.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: spring_jdbcTemplate
 * @description:
 * @author: Mr.Wang
 * @create: 2020-12-17 09:44
 **/
@Repository
public class AccountDaoImpl implements AccountDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public List<Account> findAll() {
		String sql = "select * from account";
		List<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Account>(Account.class));
		return list;
	}

	public Account findById(Integer id) {
		String sql = "select * from account where id = ?";
		Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Account>(Account.class), id);
		return account;
	}

	public void addAccount(Account account) {
		String sql = "insert into account values(null,?,?)";
		jdbcTemplate.update(sql,account.getName(),account.getMoney());
	}

	public void updateAccount(Account account) {
		String sql = "update account set name = ?,money = ? where id = ?";
		jdbcTemplate.update(sql,account.getName(),account.getMoney(),account.getId());
	}

	public void deleteById(Integer id) {
		String sql = "delete from account where id = ?";
		jdbcTemplate.update(sql,id);
	}

	public void outMoney(String outName, Double money) {
		String sql = "update account set money = money - ? where name = ?";
		jdbcTemplate.update(sql,money,outName);
	}

	public void inMoney(String inName, Double money) {
		String sql = "update account set money = money + ? where name = ?";
		jdbcTemplate.update(sql,money,inName);
	}
}
